package com.wpl.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wpl.R;
import com.wpl.user.entity.Music;
import com.wpl.user.entity.User;
import com.wpl.user.entity.vo.MusicQueryVo;
import com.wpl.user.entity.vo.UserQueryVo;
import com.wpl.user.service.MusicService;
import com.wpl.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wpl/user")
@Api(description="用户管理")
public class UserController {

	@Autowired
	private UserService userService;
	@GetMapping("findAll")
	@ApiOperation("所有用户")
	public R findAll(){
		List<User> users = userService.list(null);
		System.out.println("-------------");
		return R.ok().data("items",users);
	}
	@DeleteMapping("{id}")
	@ApiOperation("逻辑删除用户")
	public R deleteById(@ApiParam(name="id",value="讲师",required=true) @PathVariable Integer id){
		boolean flag = userService.removeById(id);
		if(flag){
			return R.ok();
		}else{
			return R.error();
		}
	}
	@GetMapping("pageFind/{current}/{limit}")
	@ApiOperation("分页查询")
	public R pageFind(@ApiParam(name="current",value="当前页",required=true) @PathVariable Integer current,@ApiParam(name="limit",value="当前页大小",required=true) @PathVariable Integer limit){
		Page page = new Page(current,limit);
		userService.page(page,null);
		Long total = page.getTotal();
		List<User> list = page.getRecords();
		return R.ok().data("total",total).data("items",list);
	}

	@PostMapping("pageCondition/{current}/{limit}")
	@ApiOperation("分页带条件查询")
	public R pageCondition(@ApiParam(name="current",value="当前页",required=true) @PathVariable Integer current, @ApiParam(name="limit",value="当前页大小",required=true) @PathVariable Integer limit, @RequestBody(required = false) UserQueryVo userQueryVo){
		Page page = new Page(current,limit);
		QueryWrapper queryWrapper = new QueryWrapper();
		String name = userQueryVo.getName();
		String begin = userQueryVo.getBegin();
		String end = userQueryVo.getEnd();
		if(!StringUtils.isEmpty(name)){
			queryWrapper.like("name",name);
		}
		if(!StringUtils.isEmpty(begin)){
			queryWrapper.ge("create_time",begin);
		}
		if(!StringUtils.isEmpty(end)){
			queryWrapper.le("update_time",end);
		}
		userService.page(page,queryWrapper);
		Long total = page.getTotal();
		List<User> list = page.getRecords();
		return R.ok().data("total",total).data("items",list);
	}

	@PostMapping("addUser")
	@ApiOperation("添加用户")
	public R addUser(@RequestBody(required = false) User user){
		boolean save = userService.save(user);
		if(save){
			return R.ok().message("保存成功");
		}else{
			return R.error().message("保存失败");
		}
	}

	@PostMapping("modifyById")
	@ApiOperation("根据id修改")
	public R modifyById(@RequestBody(required = false) User user){
		/*根据id经行修改, 但是传入的是user对象*/
		boolean save = userService.updateById(user);
		System.out.println(user);
		if(save){
			return R.ok().message("修改成功");
		}else{
			return R.error().message("修改失败");
		}
	}

	@GetMapping("selectById/{id}")
	@ApiOperation("根据id查询")
	public R selectById(@PathVariable Integer id){
		User user = userService.getById(id);
		return R.ok().data("user",user);
	}

	@PostMapping("/login")
	@ApiOperation("登录")
	public R login(@RequestBody User user){
		Integer i = userService.login(user);
		if (i != -1) return R.ok().data("id",i);
		return R.error();
	}
}

